//
//  AZLoginView.swift
//  sinaWB
//
//  Created by AzeryKiss on 16/2/25.
//  Copyright © 2016年 AzeryKiss. All rights reserved.
//

import UIKit
import SnapKit


@objc protocol sinaVisitorDelegate: NSObjectProtocol {
    
    
   optional  func didRegisterMethod()
    
   optional  func didLoginMethod()
    
}


class AZLoginView: UIView {
    
    weak var delegate: sinaVisitorDelegate?

    func  loginMethod() {
        delegate?.didLoginMethod?()
    }
    
    func registerMethod() {
        delegate?.didRegisterMethod?()
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)

    }
    
    func addLoginView(midImageStr: String, title: String, haveAnimation: Bool) {
        
        
        if haveAnimation {
            startAnimation()
        }
        
        yueshu()

        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)

        messageLabel.text = title
        
        midImage.image = UIImage(named: midImageStr)

        

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func startAnimation(){
        
        animtionImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }
  
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 10
        
        animation.toValue = M_PI * 2

        animation.removedOnCompletion = false
        
        animtionImage.layer.addAnimation(animation, forKey: nil)
    }

    func yueshu() {
        midImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }
        markView.snp_makeConstraints(closure: { (make) -> Void in
            make.center.equalTo(self.center)
        })

        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(markView.snp_bottom).offset(10)
            make.centerX.equalTo(markView.snp_centerX)
            make.width.equalTo(216)
        }
        
        register.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.width.equalTo(80)
            make.height.equalTo(30)
        }
        login.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.width.equalTo(80)
            make.height.equalTo(30)
        }


    }


    // 中间图片
    lazy var midImage: UIImageView = {
       
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        self.addSubview(img)
        
        return img
        
    }()
    
    
    // 动画图片
    lazy var animtionImage: UIImageView = {
        
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        self.addSubview(img)
        
        return img
        
    }()
    
    // 消息
    
    lazy var messageLabel: UILabel = {
       
        let label = UILabel()
        
        self.addSubview(label)
        
        label.text = "asdijfgkdahfsopjnkfi;oapsknjsopkdfjop"
        
        label.numberOfLines = 0
        
        return label
        
    }()
    
    // 注册
    
    lazy var register: UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action:"registerMethod", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.addSubview(button)
        return button
        
    }()
    
    lazy var login: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "loginMethod", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.addSubview(button)
        return button
    }()
    
    lazy var markView: UIImageView = {
        
        let v = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        self.addSubview(v)
       
        return v
    }()
    
    

}
